// 应用相关信息
import { getLicenseInfoDto } from "@/customApi/license.js"
const state = {
  authStatus: "INACTIVE",
  authCenterInfo: {
    isOnline: false, // 是否在线
    type: "INACTIVE", // 授权状态 未离线解绑->NO_UNBIND,已激活->ACTIVATED,未激活->INACTIVE
    isGetAuthStatus: false, // 是否加载过授权状态

    accreditDate: "", // 授权时间
    cancelDate: "", // 删除授权文件时间
    status: "", // 授权类型 TRIAL: 试用  TEST: 测试  FORMAL: 正式
    nowExpiryDay: null, // 剩余授权时间
    recordInfo: "", // 备案信息
    copyrightInfo: "", // 版权信息
    // 席位信息
    seatTotal: null,
    unUsedSeat: null,
    usedSeat: null,

    isGetInfo: false,
  },
}
const mutations = {
  SET_AuthCenterInfo: (state, props) => {
    state.authCenterInfo = { ...state.authCenterInfo, ...props }
  },
  SET_ActivitedStatus: (state, status) => {
    state.authStatus = status
  },
}
const actions = {
  GetAuthCenterInfo({ commit }, data) {
    return new Promise((resolve, reject) => {
      getActivitedStatus()
        .then((res) => {
          commit("SET_AuthCenterInfo", { type: res.data })
          resolve(res)
        })
        .catch((error) => {
          commit("SET_AuthCenterInfo", { type: "INACTIVE" })
          reject(error)
        })
    })
  },
  GetLicenseInfoDto({ commit }, data) {
    return new Promise((resolve, reject) => {
      getLicenseInfoDto()
        .then((res) => {
          if (res) {
            const {
              licenseInfoDto,
              status,
              type,
              seatTotal,
              unUsedSeat,
              usedSeat,
              accreditDate,
              cancelDate,
            } = res
            const { nowExpiryDay, recordInfo, copyrightInfo } = licenseInfoDto
            commit("SET_AuthCenterInfo", {
              status,
              type,
              nowExpiryDay,
              recordInfo,
              copyrightInfo,
              seatTotal,
              unUsedSeat,
              usedSeat,
              accreditDate,
              cancelDate,
            })
            resolve(res)
          } else {
            reject(new Error("Erro"))
          }
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  GetAuthorizationStatus({ commit }, data) {
    return new Promise((resolve, reject) => {
      getActivitedStatus()
        .then((res) => {
          if (res) {
            commit("SET_ActivitedStatus", res || "INACTIVE")
            resolve(res || "INACTIVE")
          } else {
            reject(new Error("Erro"))
          }
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
}

const getters = {
  getAuthCenterInfo(state) {
    return state.authCenterInfo
  },
  getActivitedStatus(state) {
    return state.authStatus
  },
}
export default {
  getters,
  state,
  mutations,
  actions,
}
